package com.whw.springboot.walk.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.whw.springboot.walk.UseToken;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.entity.TokenInfo;
import com.whw.springboot.walk.entity.User;
import com.whw.springboot.walk.service.UserInfoService;
import com.whw.springboot.walk.service.UserService;
import com.whw.springboot.walk.utils.JwtUtil;
import com.whw.springboot.walk.utils.StudentConfirmUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "用户学籍认证")
    @PostMapping(value = "/confirm")
    public Message<JSONObject> StudentConfirm(String code){
        String rs = StudentConfirmUtil.StudentConfirm(code);
        JSONObject studentInfo = JSONUtil.parseObj(rs);
        return Message.success(null).add(studentInfo);
    }


    @ApiOperation(value = "小程序帐户登陆，返回请用学号登录的提示")
    @GetMapping(value = "/login")
    public Message miniProgramLogin(String jsCode){
        return userService.miniProgramLogin(jsCode);
    }


    @ApiOperation(value = "增加用户账号")
    @PostMapping(value = "/insert")
    public Message<?> insertUser(User user){
        System.out.println(user.toString());
        return userService.insertUser(user);
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("/info")
    public Message<User> getUserInfoByToken(String token){
        TokenInfo tokenInfo = JwtUtil.getTokenInfo(token);
        return userService.selectByPrimaryKey(tokenInfo.getUserId());
    }


    @ApiOperation(value = "根据主键删除用户")
    @DeleteMapping(value = "/deletByPK")
    public Message deleteByPrimaryKey(String userId){
        return userService.deleteByPrimaryKey(userId);
    }


    @ApiOperation(value = "查询所有用户")
    @GetMapping(value = "/selectAll")
    public Message<List<User>> selectAllUser(){
        return userService.selectAllUser();
    }


    @ApiOperation(value ="查询所有用户和用户信息")
    @GetMapping(value = "/selectAllInfo")
    public Message<List<User>> selectAllUserAndInfo(){
        return userService.selectAllUserAndInfo();
    }


    @ApiOperation(value = "通过主键userId查询用户")
    @GetMapping(value = "/select")
    public Message<User>selectByPrimaryKey1(String userId){
        return userService.selectByPrimaryKey1(userId);
    }


    @ApiOperation(value = "通过userId和userPwd查询用户")
    @GetMapping(value = "/selectByIdAndPwd")
    public Message<User> selectByUserIdAndUserPwd(String userId, String userPwd){
        return userService.selectByUserIdAndUserPwd(userId,userPwd);
    }


    @ApiOperation(value = "通过主键修改用户")
    @PutMapping(value = "/updateByPK")
    public Message updateByPrimaryKey(User user){
        return userService.updateByPrimaryKey(user);
    }


    @ApiOperation(value = "通过主键动态修改用户")
    @PutMapping(value = "/updateByPKSelective")
    public Message updateByPrimaryKeySelective(User user){
        return userService.updateByPrimaryKeySelective(user);
    }


    @ApiOperation(value = "通过主键修改用户密码")
    @PutMapping(value = "/changePwd")
    public Message changePwd(String user_id,String user_pwd){
        return userService.changePwd(user_id, user_pwd);
    }


    @ApiOperation(value="管理端用户登录")
    @PostMapping("/Login")
    public Message adminLogin(String userId,String password){
        return userService.Login(userId, password);
    }


    @ApiOperation(value = "管理端新增用户和用户信息")
    @PostMapping(value = "/addManagerUser")
    @UseToken(level = 1)
    public Message addManagerUser(User user){
        return userService.addManagerUser(user);

    }


    @ApiOperation(value = "管理端封禁用户")
    @PutMapping(value = "/banManagerUser")
    @UseToken(level = 1)
    public Message banManagerUser(User user){
        return userService.banManagerUser(user);
    }


    @ApiOperation(value = "管理端修改用户是否加入老乡会")
    @PutMapping(value = "/changeOldStatus")
    @UseToken(level = 1)
    public Message changeOldStatus(User user){
        return userService.changeOldStatus(user);
    }


    @ApiOperation(value = "判断用户是否加入老乡会")
    @GetMapping(value = "/selectByUserIdOldStatus")
    public Message selectByUserIdOldStatus(String userId){
        return userService.selectByUserIdOldStatus(userId);
    }
}
